package com.supor.aiot.home.apdater;


import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.flexbox.FlexboxLayout;
import com.supor.aiot.home.R;
import com.supor.aiot.home.bean.ChiefItem;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.core.content.res.ResourcesCompat;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: ChiefAdapter <br>
 * 作者: lei <br>
 * 日期: 2021/7/16 <br>
 */
public class ChiefAdapter extends BaseQuickAdapter<ChiefItem, BaseViewHolder> {
    String[] goodAt1 = new String[]{"川菜", "湘菜"};
    String[] goodAt2 = new String[]{"川菜", "湘菜", "粤菜", "江浙菜"};

    public ChiefAdapter(List<ChiefItem> dataList) {
        super(R.layout.item_chief, dataList);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ChiefItem data) {
        int position = holder.getAdapterPosition();
        FlexboxLayout flexboxLayout = holder.itemView.findViewById(R.id.fbl_good_at);
        if (position % 2 == 0) {
            holder.itemView.setBackgroundResource(R.drawable.bg_chief_item3);
            holder.setTextColorRes(R.id.tv_recipe_num, R.color.color_319BE0);
            holder.setImageResource(R.id.iv_arrow, R.mipmap.ic_chief_arrow3);
            holder.setTextColorRes(R.id.tv_grade, R.color.color_319BE0);
            // addRecipeClassify(flexboxLayout, goodAt1, R.drawable.bg_chief_item3_corner, R.color.color_319BE0);
        } else if (position % 2 == 1) {
            holder.itemView.setBackgroundResource(R.drawable.bg_chief_item5);
            holder.setTextColorRes(R.id.tv_recipe_num, R.color.colorPrimary);
            holder.setImageResource(R.id.iv_arrow, R.mipmap.ic_chief_arrow5);
            holder.setTextColorRes(R.id.tv_grade, R.color.colorPrimary);
            // addRecipeClassify(flexboxLayout, goodAt2, R.drawable.bg_chief_item5_corner, R.color.colorPrimary);
        }

        holder.setImageResource(R.id.iv_icon, data.imgResource);
        holder.setText(R.id.tv_name, data.name);
        if (TextUtils.isEmpty(data.newRecipe)) {
            holder.setText(R.id.tv_recipe_num, ("累计创作" + data.recipeNum + "道食谱"));
        } else {
            holder.setText(R.id.tv_recipe_num, ("今日创作" + data.newRecipe));
        }
        TextView grade = holder.itemView.findViewById(R.id.tv_grade);
        grade.setText(data.grade);
        if ("签约主厨".equalsIgnoreCase(data.grade)) {
            grade.setCompoundDrawablesWithIntrinsicBounds(
                    ResourcesCompat.getDrawable(holder.itemView.getResources(), R.mipmap.ic_create_star, null),
                    null, null, null);
        } else if ("达人创作".equalsIgnoreCase(data.grade)) {
            grade.setCompoundDrawablesWithIntrinsicBounds(
                    ResourcesCompat.getDrawable(holder.itemView.getResources(), R.mipmap.ic_create_expert, null),
                    null, null, null);
        }
    }

    private void addRecipeClassify(FlexboxLayout flexboxLayout, String [] goodAt, int drawable, int color) {
        for (String s : goodAt) {
            LinearLayout layout = (LinearLayout) LayoutInflater.from(flexboxLayout.getContext())
                    .inflate(R.layout.chief_text, null);
            TextView textview = (TextView) layout.findViewById(R.id.tv_recipe_text);
            textview.setBackgroundResource(drawable);
            textview.setTextColor(flexboxLayout.getContext().getResources().getColor(color));
            textview.setText(s);
            flexboxLayout.addView(layout);
        }
    }
}